// Copyright 2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.apps.moderator;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

/**
 * This class wraps the AccountManager functionality which may not be present
 * on all devices. It is used effectively as an extension of LoginActivity.
 */
public class AccountManagerWrapper {

  protected static final String TAG = AccountManagerWrapper.class.getName();

  static Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
      account = accounts[0];
    } else {
      account = null;
    }
    return account;
  }

  public static void tryGetAuthToken(final LoginActivity activity) {
    AccountManager accountManager = AccountManager.get(activity);   
    Account account = getAccount(accountManager);
    if (account != null) {
      accountManager.getAuthToken(account, "moderator", true, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> arg0) {
          try {
            Bundle result = arg0.getResult();
            if (result.containsKey("authtoken")) {
              activity.setAuthToken(result.getString("authtoken"));
            } else {
              Intent intent = (Intent) result.get("intent");
              intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
              activity.startActivityForResult(intent, 0);
            }
          } catch (IllegalArgumentException e) { 
            Log.e(TAG, "An exception occurred", e);
          } catch (OperationCanceledException e) {
            Log.e(TAG, "An exception occurred", e);
          } catch (AuthenticatorException e) {
            Log.e(TAG, "An exception occurred", e);
          } catch (IOException e) {
            Log.e(TAG, "An exception occurred", e);
          }
        }
      }, null);
    }
  }
  
  static String getEmail(Context context) {
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);

    if (account == null) {
      return null;
    } else {
      return account.name;
    }
  }
}
